package com.ruoyi.tb.lmt.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.tb.lmt.domain.bo.TbMutualInspectionBo;
import com.ruoyi.tb.lmt.domain.bo.TbMutualInspectiondetailBo;
import com.ruoyi.tb.lmt.domain.bo.TbSpecialInspectionBo;
import com.ruoyi.tb.lmt.domain.vo.TbMutualInspectiondetailVo;
import com.ruoyi.tb.lmt.service.ITbSpecialInspectionService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.tb.lmt.domain.vo.TbSpecialInspectiondetailVo;
import com.ruoyi.tb.lmt.domain.bo.TbSpecialInspectiondetailBo;
import com.ruoyi.tb.lmt.service.ITbSpecialInspectiondetailService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 专检详情
 *
 * @author ruoyi
 * @date 2024-01-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xha-lmt/specialInspectiondetail")
public class TbSpecialInspectiondetailController extends BaseController {

    private final ITbSpecialInspectiondetailService iTbSpecialInspectiondetailService;

    @Autowired
    private ITbSpecialInspectionService iTbSpecialInspectionService;
    /**
     * 查询专检详情列表
     */
    @GetMapping("/list")
    public TableDataInfo<TbSpecialInspectiondetailVo> list(TbSpecialInspectiondetailBo bo, PageQuery pageQuery) {
        return iTbSpecialInspectiondetailService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出专检详情列表
     */
    @Log(title = "专检详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TbSpecialInspectiondetailBo bo, HttpServletResponse response) {
        List<TbSpecialInspectiondetailVo> list = iTbSpecialInspectiondetailService.queryList(bo);
        ExcelUtil.exportExcel(list, "专检详情", TbSpecialInspectiondetailVo.class, response);
    }

    /**
     * 获取专检详情详细信息
     *
     * @param ID 主键
     */
    @SaCheckPermission("xha-lmt:specialInspectiondetail:query")
    @GetMapping("/{ID}")
    public R<TbSpecialInspectiondetailVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long ID) {
        return R.ok(iTbSpecialInspectiondetailService.queryById(ID));
    }

    /**
     * 新增专检详情
     */
    @SaCheckPermission("xha-lmt:specialInspectiondetail:add")
    @Log(title = "专检详情", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TbSpecialInspectiondetailBo bo) {
        return toAjax(iTbSpecialInspectiondetailService.insertByBo(bo));
    }

    /**
     * 修改专检详情
     */
    @SaCheckPermission("xha-lmt:specialInspectiondetail:edit")
    @Log(title = "专检详情", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TbSpecialInspectiondetailBo bo) {
        return toAjax(iTbSpecialInspectiondetailService.updateByBo(bo));
    }

    /**
     * 删除专检详情
     *
     * @param IDs 主键串
     */
    @SaCheckPermission("xha-lmt:specialInspectiondetail:remove")
    @Log(title = "专检详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] IDs) {
        return toAjax(iTbSpecialInspectiondetailService.deleteWithValidByIds(Arrays.asList(IDs), true));
    }

    @SaCheckPermission("xha-lmt:specialInspectiondetail:addSpecial")
    @Log(title = "新增专检详情任务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/addSpecial")
    public R<Void> addSpecial(@Validated(AddGroup.class) @RequestBody Map map) {
        try {
            //修改专检主表信息
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            TbSpecialInspectionBo tbSpBo = new TbSpecialInspectionBo();
            tbSpBo.setId(map.get("mutualId")==null?null:Long.parseLong((String) map.get("mutualId")));
            String requireStartTime = (String) map.get("repairStartDate");
            if (requireStartTime!= null &&!requireStartTime.isEmpty()) {
                tbSpBo.setRepairStartDate(formatter.parse(requireStartTime));
            }
            iTbSpecialInspectionService.updateByBo(tbSpBo);
            //新增专检详情
            TbSpecialInspectiondetailBo tbSpecialBo = new TbSpecialInspectiondetailBo();
            tbSpecialBo.setMutualId(map.get("mutualId")==null?null:Long.parseLong((String) map.get("mutualId")));
            tbSpecialBo.setWorkName((String) map.get("workName"));
            tbSpecialBo.setWorkKind((String) map.get("workKind"));
            String workGroupIdString = (String) map.get("workCategoryId");
            if (workGroupIdString != null && !workGroupIdString.isEmpty()) {
                try {
                    Long workGroupId = Long.parseLong(workGroupIdString);
                    tbSpecialBo.setWorkCategoryId(workGroupId);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
            tbSpecialBo.setWorkCategory((String) map.get("workCategory"));
            tbSpecialBo.setWorkCategoryRemark((String) map.get("workCategoryRemark"));
            tbSpecialBo.setStatus("未提报");
            tbSpecialBo.setCheckReapirReqire((String) map.get("checkReapirReqire"));
            tbSpecialBo.setTaskUrl((String) map.get("taskUrl"));
            tbSpecialBo.setCreateTime(new Date());
            LoginUser currentUser =  LoginHelper.getLoginUser();
            tbSpecialBo.setCreateBy(currentUser.getUsername());
            iTbSpecialInspectiondetailService.insertByBo(tbSpecialBo);
            return R.ok();
        }
        catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    @SaCheckPermission("xha-lmt:specialInspectiondetail:updateSpecial")
    @Log(title = "修改专检任务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/updateSpecial")
    public R<Void> updateSpecial(@Validated(AddGroup.class) @RequestBody Map map) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            TbSpecialInspectionBo tbSpBo = new TbSpecialInspectionBo();
            tbSpBo.setId(map.get("mutualId")==null?null:Long.parseLong((String) map.get("mutualId")));
            tbSpBo.setRepairStartDate(map.get("repairStartDate")==null?null:formatter.parse((String) map.get("repairStartDate")));
            iTbSpecialInspectionService.updateByBo(tbSpBo);
            TbSpecialInspectiondetailBo tbSpecialBo = new TbSpecialInspectiondetailBo();
            LoginUser currentUser =  LoginHelper.getLoginUser();
            tbSpecialBo.setID(map.get("id")==null?null:Long.parseLong((String) map.get("id")));
            tbSpecialBo.setMutualId((String) map.get("mutualId")==null?null:Long.parseLong((String) map.get("mutualId")));
            tbSpecialBo.setWorkName((String) map.get("workName"));
            tbSpecialBo.setWorkKind((String) map.get("workKind"));
            tbSpecialBo.setWorkCategoryId(map.get("workCategoryId")==null?null:Long.parseLong((String) map.get("workCategoryId")));
            tbSpecialBo.setWorkCategory((String) map.get("workCategory"));
            tbSpecialBo.setWorkCategoryRemark((String) map.get("workCategoryRemark"));
            tbSpecialBo.setCheckReapirReqire((String) map.get("checkReapirReqire"));
            tbSpecialBo.setRemark((String) map.get("remark"));
            tbSpecialBo.setWorkGroup((String) map.get("workGroup"));
            tbSpecialBo.setWorkUser((String) map.get("workUser"));
            tbSpecialBo.setWorkUser2((String) map.get("workUser2"));
            tbSpecialBo.setTaskUrl((String) map.get("taskUrl"));
            tbSpecialBo.setUpdateTime(new Date());
            tbSpecialBo.setUpdateBy(currentUser.getUsername());

            iTbSpecialInspectiondetailService.updateByBo(tbSpecialBo);
            return R.ok();
        }
        catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    //查询专检任务
    @GetMapping("/list/selectSpecial")
    public TableDataInfo<TbSpecialInspectiondetailVo> selectSpecial(Long mutualId, String lmtType, String lmtNumber, String status, PageQuery pageQuery) {
        return iTbSpecialInspectiondetailService.selectSpecial(mutualId,lmtType,lmtNumber,status, pageQuery);
    }
}
